package com.orangelife.mobile.washcar.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.net.NetworkUtil;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.MoneyUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.goodsrent.activity.GoodsRentOrderDetailActivity;
import com.orangelife.mobile.pay.activity.UsersPaidActivity;
import com.orangelife.mobile.pay.interfaze.IOuttradenoCallBack;
import com.orangelife.mobile.pay.biz.PayManageImpl;
import com.orangelife.mobile.pay.biz.PaymentModule;
import com.orangelife.mobile.main.activity.ServiceOrderActivity;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.washcar.activity.WashCarDetailActivity;
import com.orangelife.mobile.widget.ScrollListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class WashCarOrderFirstListViewAdapter<T> extends FatherAdapter<T> {
    private Context context;
    private List<Map<String, Object>> list;
    private List<Map<String, Object>> goods;
    private ImageDownloader image = ImageDownloader.getImageDownloader();

    public WashCarOrderFirstListViewAdapter(Context context,
                                            List<Map<String, Object>> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_mygoods_order_parent, null);
            viewHolder.llMyGoods = (LinearLayout) convertView
                    .findViewById(R.id.llMyGoodsItem);
            viewHolder.tvRemainTime = (TextView) convertView
                    .findViewById(R.id.tvRemainTime);
            viewHolder.tvStatus = (TextView) convertView
                    .findViewById(R.id.tvStatus);
            viewHolder.rlTotalPrice = (RelativeLayout) convertView
                    .findViewById(R.id.rlTotalPrice);
            viewHolder.tvMoney = (TextView) convertView
                    .findViewById(R.id.tvMoney);
            viewHolder.lvChildren = (ScrollListView) convertView.findViewById(R.id.lvChildren);
            viewHolder.btnConfirmPaid = (Button) convertView
                    .findViewById(R.id.btnConfirmPaid);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvRemainTime.setText("DD洗车");
        String orderStatus = list.get(position)
                .get("orderStatus").toString();
        String orderStatusStr = "";
        switch (orderStatus) {
            case "0":
                orderStatusStr = "待付款";
                break;
            case "1":
                orderStatusStr = "已付款";
                break;
            case "2":
                orderStatusStr = "已超时";
                break;
            case "3":
                orderStatusStr = "已派单";
                break;
            case "4":
                orderStatusStr = "已取消";
                break;
            case "5":
                orderStatusStr = "服务中";
                break;
            case "6":
                orderStatusStr = "已完成";
                break;
            case "8":
                orderStatusStr = "已退款";
                break;

            default:
                break;
        }
        viewHolder.tvStatus.setText(orderStatusStr);
        Map<String, Object> map = list.get(position);
        viewHolder.tvMoney.setText("¥" + MoneyUtil.handleMoney(list.get(position).get("totalCost")
                .toString()));
        if ("0".equals(list.get(position).get("orderStatus").toString())) {
            viewHolder.btnConfirmPaid.setVisibility(View.VISIBLE);
        } else {
            viewHolder.btnConfirmPaid.setVisibility(View.GONE);
        }
        if (!StringUtil.isBlank(map.get("orderInfo").toString())) {
            goods = (List<Map<String, Object>>) map.get("orderInfo");
        } else {
            goods = new ArrayList<>();
        }
        viewHolder.btnConfirmPaid.setOnClickListener(new listener(map));
        viewHolder.llMyGoods.setOnClickListener(new listener(map));
        viewHolder.llMyGoods.setOnLongClickListener(longListener);
        if (goods == null) {
            goods = new ArrayList<>();
        }
        if (viewHolder.adapter == null) {
            viewHolder.adapter = new WashCarOrderListChildrenAdapter<>(context, goods);
            viewHolder.lvChildren.setAdapter(viewHolder.adapter);
        } else {
            viewHolder.adapter.setList(goods);
            viewHolder.adapter.notifyDataSetChanged();
        }
        viewHolder.lvChildren.setOnItemClickListener(new itemListener(map));
        return convertView;

    }

    private class itemListener implements AdapterView.OnItemClickListener {
        private Map<String, Object> map = null;

        public itemListener(Map<String, Object> map) {

            this.map = map;
        }

        Intent intent = new Intent();
        Bundle bundle = new Bundle();

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            intent.putExtra("orderID", map.get("orderID").toString());
            intent.setClass(context, WashCarDetailActivity.class);
            context.startActivity(intent);
        }
    }

    /**
     * 长按删除
     */
    private OnLongClickListener longListener = new OnLongClickListener() {
        @Override
        public boolean onLongClick(View arg0) {
            return false;
        }

    };

    class listener implements View.OnClickListener {
        private Map<String, Object> map = null;

        public listener(Map<String, Object> map) {

            this.map = map;
        }

        Intent intent = new Intent();
        Bundle bundle = new Bundle();

        @Override
        public void onClick(View v) {
            if (!NetworkUtil.isConnectedNet(context)) {
                Toast.makeText(
                        context,
                        context.getResources().getString(
                                R.string.net_disconnected), Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            switch (v.getId()) {
                case R.id.llMyGoodsItem:
                    intent.putExtra("orderID", map.get("orderID").toString());
                    intent.setClass(context, GoodsRentOrderDetailActivity.class);
                    context.startActivity(intent);
                    break;
                case R.id.btnConfirmPaid:
                    /**获取流水号回调方法*/
                    PayManageImpl payManageImpl = new PayManageImpl(new IOuttradenoCallBack() {
                        @Override
                        public void data(JSONObject jsonObject) {
                            ExitApplication.getInstance().addMainActivity((ServiceOrderActivity) context);
                            try {
                                Map<String, Object> entity = JSONHelper.jsonToMap(jsonObject.get("entity").toString());
                                bundle.putString("outtradeno", entity.get("outtradeno").toString());
                                bundle.putString("points", entity.get("points").toString());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            bundle.putString("orderInfo", "DD洗车");
                            bundle.putString("totalPrice", Constant.RMB_FLAG + String.valueOf(map.get("totalCost")));
                            bundle.putString("totalCost", String.valueOf(map.get("totalCost")));
                            bundle.putString("orderID", String.valueOf(map.get("orderID")));
                            bundle.putString("status", "goods");
                            bundle.putBoolean("orderList", true);/**支付成功后返回商品列表*/
                            bundle.putString(PaymentModule.PAID_METHOD, PaymentModule.mode[11]);
                            bundle.putString("orderType", String.valueOf(map.get("orderType")));
                            intent.putExtras(bundle);
                            intent.setClass(context, UsersPaidActivity.class);
                            context.startActivity(intent);
                        }
                    });
                    payManageImpl.getOuttradenoFromServer(map.get("orderID").toString(), 16);

                    break;

                default:
                    break;
            }
        }
    }

    class ViewHolder {
        private LinearLayout llMyGoods;
        private TextView tvRemainTime;
        private TextView tvStatus;
        private RelativeLayout rlTotalPrice;
        private TextView tvMoney;
        private Button btnConfirmPaid;
        private ScrollListView lvChildren;
        WashCarOrderListChildrenAdapter<Map<String, Object>> adapter;
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        this.list = (List<Map<String, Object>>) list;
        return WashCarOrderFirstListViewAdapter.this;
    }

}
